<?php
/**
 * 登录接口
 * @author Edwin
 */

namespace V1\Controller;

use Common\Controller\ApiController;
use Think\Exception;
use V1\Model\MemberModel;

class IndexController extends ApiController
{

    public function _initialize()
    {
        //模块初始化，重写父类方法，避免该模块进入token验证
    }

    /**
     * 登录接口
     * 判断mobile是否为空或者为null
     * 如果为空或者为null则输出请填写帐号
     * 判断提交的密码的双重md5值与数据值是否相同相同则继续执行
     * 否则提示账号或密码错误
     */
    public function login()
    {
        $user_mobile = I('post.mobile');
        $password = md5(md5(I('post.password')));
        $from = I('post.from', 'android');
        if ($user_mobile == "" || $user_mobile == null) {
            $this->myApiPrint('请填写帐号', 400);
        }
        $where['passsword'] = $password;
        $where['mobile'] = $user_mobile;
        $owner = D('Member');
        $resn = $owner->where($where)->field('userid,mobile,password,status,addtime,updatetime,appid,last_login_time')->find();
        if ($password != $resn['password']) {
            $this->myApiPrint('帐号密码错误', 300);
        } else {
            if (empty($resn['updatetime'])) {
                $msg = 'first login';
            } else {
                $msg = 'success';
            }
            $ins_data['from'] = $from;
            if ($resn['status'] != 0) {
                //更新积分,积分增加2
                $last_time = $resn['last_login_time'];
                $timestamp = time() - $last_time;
                if ($timestamp > 86400) {
                    $show['integral'] = $this->add_score($resn['userid']);
                }
                //更新访问时间
                $this->updateLastLoginTime($resn['userid']);
                //获取用户是否完善个人信息
                $show['info'] = $this->getMemAreaId($resn['userid']);
                $owner->where('stu_id=' . $resn ['stu_id'])->data($ins_data)->save();
                $strToken = $user_mobile . '|' . $resn['userid'];
                $resn['token'] = myDes_encode($strToken, $user_mobile);
                $this->myApiPrint($msg, 200, $resn);
            } else {
                $this->myApiPrint('帐号密码错误', 300);
            }
        }
    }

    public function resetPassword()
    {
        $member_model = new MemberModel();
        $mobile = trim(I('post.mobile'));
        $password = md5(md5(I('post.password')));
        try {
            $result = $member_model->updatePassword($password, $mobile);
            if ($result) {
                $this->myApiPrint(C('STATUS')['200'], 200, true);
            } else {
                $this->myApiPrint(C('STATUS')['208'], 201, false);
            }
        } catch (Exception $exception) {
            $this->myApiPrint(C('STATUS')['101'], 400, false);
        }
    }

    /**
     * 更新积分
     * @param $userid
     * @return bool
     */
    private function add_score($userid)
    {
        $map['userid'] = $userid;
        return D('MemberProfile')->where($map)->setInc('score', 2);
    }

    /**
     * 更新上次登录时间
     * @param $userid
     * @return bool
     */
    private function updateLastLoginTime($userid)
    {
        $map['userid'] = $userid;
        $data['last_login_time'] = time();
        return D('Member')->where($map)->save($data);
    }

    /**
     * 获取用户是否完善个人信息
     * @param $userid [用户ID]
     * @return int     返回 int 【1 已经完善   0 未完善】
     */
    private function getMemAreaId($userid)
    {
        $map['userid'] = $userid;
        $data = D('MemberProfile')->where($map)->getField('areaid');
        if ($data > 0) {
            return 1;  //已存在
        } else {
            return 0;  //未存在
        }
    }
}
